<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib  prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib  prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="srcPath" value="${redis:getSysConfig('sysFileVisitRoot_back')}"/>
<c:set var="wx_ticket" value="${redis:getRedisValue('wx_ticket')}"/>
<c:set var="wx_access_tokens" value="${redis:getRedisValue('wx_access_tokens')}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>微信菜单</title>
        <jsp:include page="../../util/import.jsp"/>
        <link rel="stylesheet" type="text/css" href="${srcPath}/wxMenu/css/menu.css" media="all">
        <script type="text/javascript" src="${srcPath}/wxMenu/js/nav4.js"></script>
        <script type="text/javascript">
            $(function () {
                initMenuInfo();
            });

            function initMenuInfo() {
                $(function () {
                    $.ajax({
                        async:false,
                        type: "POST",
                        dataType: "JSON",
                        url: "${basePath}/back/wx/menuInfo/listMenuInfo.sjson",
                        success: function(result){
                            var firstWxMenuInfoList = JSON.parse(result.data);
                            var menuUl_html="";
                            if(firstWxMenuInfoList!=null && firstWxMenuInfoList!=undefined && firstWxMenuInfoList.length>0){
                                for(var i=0;i<firstWxMenuInfoList.length;i++){
                                    var firstWxMenuInfo=firstWxMenuInfoList[i];
                                    var first_wxmiId=firstWxMenuInfo.wxmiId;
                                    var secondWxMenuInfoList=firstWxMenuInfo.subWxMenuList;
                                    var isOn="";
                                    if(i==firstWxMenuInfoList.length-1){ /**默认最后子菜单的二级菜单是打开状态**/
                                    isOn="on";
                                    }
                                    menuUl_html+="<li>";
                                    menuUl_html+="<a href='javascript:' class='"+isOn+"'>";
                                    if(secondWxMenuInfoList.length>0){ /**没有子菜单将不显示左侧列表的小图标**/
                                    menuUl_html+="<span class='glyphicon glyphicon-th-list'></span>";
                                    }
                                    menuUl_html+="<span data-toggle='modal' data-toggle='tooltip' data-placement='top' title='编辑此菜单' id='btn_edit_"+i+"' onclick='editMenu("+first_wxmiId+","+i+")'>"+firstWxMenuInfo.name+"</span>&nbsp;";
                                    menuUl_html+="<span data-toggle='modal' data-toggle='tooltip' data-placement='top' title='新增二级菜单' class='glyphicon glyphicon-plus' aria-hidden='true' id='btn_addSubMenu_"+i+"' onclick='addSubMenu("+first_wxmiId+","+i+")'></span>";
                                    menuUl_html+="<span data-toggle='modal' data-toggle='tooltip' data-placement='top' title='删除此菜单' class='glyphicon glyphicon-remove' aria-hidden='true' onclick='deleteMenu("+first_wxmiId+")'></span>";
                                    menuUl_html+="</a>";

                                    if(secondWxMenuInfoList!=null && secondWxMenuInfoList!=undefined && secondWxMenuInfoList.length>0){
                                        menuUl_html+="<dl>";
                                        for(var j=0;j<secondWxMenuInfoList.length;j++){
                                            var secondWxMenuInfo=secondWxMenuInfoList[j];
                                            var second_wxmiId=secondWxMenuInfo.wxmiId;
                                            menuUl_html+="<dd>";
                                            menuUl_html+="<a href='#'><span data-toggle='modal' data-toggle='tooltip' data-placement='top' title='编辑此88菜单' id='btn_edit_"+(5*i+j+4)+"' onclick='editMenu("+second_wxmiId+","+(5*i+j+4)+")'>"+secondWxMenuInfo.name+"</span>&nbsp;";
                                            menuUl_html+="<span data-toggle='modal' data-toggle='tooltip' data-placement='top' title='删除此菜单' class='glyphicon glyphicon-remove' aria-hidden='true' onclick='deleteMenu("+second_wxmiId+")'></span>";
                                            menuUl_html+="</a></dd>";
                                        }
                                        menuUl_html+="</dl>";
                                    }
                                    menuUl_html+="</li>";
                                }
                                $("#menuUl").empty().html(menuUl_html);
                            }else{
                                $("#menuUl").empty().html("<li><a href='javascript:' class=''><span>无菜单信息，请新增菜单！</span></a></li>");
                            }
                            /**初始化弹出动作**/
                            nav4.bindOnMouseOver(document.getElementById("nav4_ul").querySelectorAll("li>a"), document.getElementById("nav4_masklayer"));
                        }
                    });
                });
            }

            function deleteMenu(wxmiId){/**删除当前菜单信息**/
                showConfirm("你确定要删除吗?", function() {
                    $.ajax({
                        type: "POST",
                        dataType: "JSON",
                        url: "${basePath}/back/wx/menuInfo/deleteMenuInfo.sjson",
                        data: {wxmiId:wxmiId},
                        success: function(data){
                            if(data.success==true){
                                /**刷新手机页面**/
                                initMenuInfo();
                            }
                        }
                    });
                });
            }

            /**同步微信访问令牌信息**/
            <shiro:hasPermission name="back_gotoMenuInfoPage_accessTokenUpdate">
            function accessTokenUpdate(){
                showConfirm("你确定要同步更新吗?", function() {
                    $.ajax({
                        type: "POST",
                        dataType: "JSON",
                        url: "${basePath}/back/wx/publicNo/wxPublicNoInfoAccessTokenUpdate.sjson",
                        success: function(data){
                            if(data.success==true){
                                /**刷新手机页面**/
                                showMsg("同步成功，"+data.msg,function() {initMenuInfo()});
                            }else{
                                showMsg("同步失败，请重新同步更新！",initMenuInfo());
                            }
                        }
                    });
                });
            }
            </shiro:hasPermission>

            /**同步生成微信服务器菜单**/
            <shiro:hasPermission name="back_gotoMenuInfoPage_sync">
            function createWxMenu(){
                $.ajax({
                    type: "POST",
                    dataType: "JSON",
                    url: "${basePath}/back/wx/menuInfo/createWxMenu.sjson",
                    success: function(result){
                        showMsg(result.data);
                    }
                });
            }
            </shiro:hasPermission>

            /**删除微信服务器菜单信息**/
            <shiro:hasPermission name="back_gotoMenuInfoPage_deleteServer">
            function deleteWxMenu(){
                $.ajax({
                    type: "POST",
                    dataType: "JSON",
                    url: "${basePath}/back/wx/menuInfo/deleteWxMenu.sjson",
                    success: function(result){
                        showMsg(result.data);
                    }
                });
            }
            </shiro:hasPermission>

            /**查看微信AccessToken令牌信息**/
            <shiro:hasPermission name="back_gotoMenuInfoPage_getAccessToken">
            function getAccessToken(){
                showMsg("查看微信AccessToken令牌信息：" + "${wx_access_tokens}");
            }
            </shiro:hasPermission>

            /**查看本地存储ticket信息**/
            <shiro:hasPermission name="back_gotoMenuInfoPage_getTicket">
            function getTicket(){
                showMsg("查看微信获取Ticket信息：" + "${wx_ticket}");
            }
            </shiro:hasPermission>

        </script>
    </head>

    <body>
        <div id="menuWrap" class="panel-body">
            <div id="menu">
                <div id="addMenu">
                    <shiro:hasPermission name="back_gotoMenuInfoPage_addFirst">
                        <button id="btn_add" type="button" class="btn btn-primary" data-toggle="modal" onclick="addInfo();">
                            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增一级微信菜单信息&nbsp;&nbsp;&nbsp;&nbsp;
                        </button><br><br>
                    </shiro:hasPermission>
                    <shiro:hasPermission name="back_gotoMenuInfoPage_preview">
                        <button id="btn_preview" type="button" class="btn btn-primary" data-toggle="modal" data-target="#detailModal" onclick="previewWxMenu();">
                            <span class="glyphicon glyphicon-search" aria-hidden="true"></span>菜单生成前数据信息预览
                        </button><br><br>
                    </shiro:hasPermission>
                    <shiro:hasPermission name="back_gotoMenuInfoPage_sync">
                        <button id="btn_sync" type="button" class="btn btn-primary" data-toggle="modal" onclick="createWxMenu();">
                            <span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>同步生成微信服务器菜单
                        </button><br><br>
                    </shiro:hasPermission>
                    <shiro:hasPermission name="back_gotoMenuInfoPage_get">
                        <button id="btn_get" type="button" class="btn btn-primary" data-toggle="modal" data-target="#detailModal" onclick="getWxMenu();">
                            <span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>获取微信服务器菜单信息
                        </button><br><br>
                    </shiro:hasPermission>
                    <shiro:hasPermission name="back_gotoMenuInfoPage_deleteServer">
                        <button id="btn_delete" type="button" class="btn btn-primary" data-toggle="modal" onclick="deleteWxMenu();">
                            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除微信服务器菜单信息
                        </button><br><br>
                    </shiro:hasPermission>
                    <shiro:hasPermission name="back_gotoMenuInfoPage_accessTokenUpdate">
                        <button id="btn_update_access_token" type="button" class="btn btn-primary" data-toggle="modal" onclick="accessTokenUpdate();">
                            <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span>同步微信服务器令牌信息
                        </button><br><br>
                    </shiro:hasPermission>
                    <shiro:hasPermission name="back_gotoMenuInfoPage_getAccessToken">
                        <button id="btn_get_access_token" type="button" class="btn btn-primary" data-toggle="modal" onclick="getAccessToken();">
                            <span class="glyphicon glyphicon-search" aria-hidden="true"></span>查看微信accesstoken信息
                        </button><br><br>
                    </shiro:hasPermission>
                    <shiro:hasPermission name="back_gotoMenuInfoPage_getTicket">
                        <button id="btn_get_ticket" type="button" class="btn btn-primary" data-toggle="modal" onclick="getTicket();">
                            <span class="glyphicon glyphicon-barcode" aria-hidden="true"></span>查看微信获取Ticket信息&nbsp;
                        </button>
                    </shiro:hasPermission>
                </div>
                <div data-role="widget" data-widget="nav4" class="nav4">
                    <div class="wx_qrcode">
                        <div class="wx_qrcode_center">
                            <h3><span class="label label-info">微信公众号二维码</span></h3>
                            <div class="wx_qrcode_top">
                                <a data-toggle="tooltip" data-placement="bottom" href="javascript:"
                                   title="微信公众号二维码图片，如果二维码没有显示，请点击右侧'同步微信服务器令牌信息'按钮" >
                                    <img class="wx_qrcode_img" src="${qrCodeURL}" onerror="this.src='${srcPath}/common/images/mrbj.png'"  />
                                </a>
                            </div>
                        </div>
                    </div>
                    <nav>
                        <div id="nav4_ul" class="nav_4">
                            <ul class="box" id="menuUl"></ul>
                        </div>
                    </nav>
                    <div id="nav4_masklayer" class="masklayer_div on">&nbsp;</div>
                </div>
            </div>
        </div>
        <!-- 添加修改页面 -->
        <jsp:include page="addOrEditor.jsp"/>
        <!-- 微信菜单详情页面 -->
        <jsp:include page="detail.jsp"/>
    </body>
</html>
